package database;

/**
 * Login:
 * Users select their user type, enter their ID number and password
 * if necessary, and are directed to their GUI.
 * @author Samir Harb
 *
 */

import gui.GUI;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Login extends JPanel
                        implements ActionListener {

    public static JFrame frame;
    GUI gui;
	/**
	 * @param args
	 */
    public Login()
    {
        JButton admin = new JButton("Administrator");
        JButton inst = new JButton("Instructor");
        JButton student = new JButton("Student");

        admin.setActionCommand("administrator");
        inst.setActionCommand("instructor");
        student.setActionCommand("student");

        admin.addActionListener(this);
        inst.addActionListener(this);
        student.addActionListener(this);

        add(admin);
        add(inst);
        add(student);

        gui = new GUI();
    }

    //Starts the whole program.
	public static void main(String[] args)
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                startLogin();
            }
        });

	}

    /**
     * This starts the login screen by adding the three buttons and the background
     * image.
     */
    public static void startLogin()
    {
        String bgfile = "images/title.jpg";
        frame = new JFrame("Choose User Type:");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel bgimage = new JLabel();
                bgimage.setIcon(new javax.swing.ImageIcon(bgfile));
        Login lg = new Login();
        lg.setOpaque(true); //content panes must be opaque
        frame.getContentPane().add(bgimage, BorderLayout.PAGE_START);
        frame.getContentPane().add(lg);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null); //centers it on screen
    }

    /**
     * Prompts the admin for his ID number and password
     * and logs him into the system if he passes verification.
     *
     */
    public void adminLogin()
    {
        Database db = new Database();
        while(true)
        {
            String ID = JOptionPane.showInputDialog(null, "Enter ID number:");
            try
            {
                if(ID.equals(null))
                    break;

            }catch(NullPointerException e){ break;}
            JPasswordField pass = new JPasswordField();
            JOptionPane.showMessageDialog (null, pass, "Enter password", JOptionPane.OK_OPTION);
            char[] chars = pass.getPassword();
            String pw = new String(chars);
            if(db.verifyUser(ID, pw, 2))
            {
                //open admin interface
                gui.initComponents();
                gui.runAdminUI(gui.getMainFrame());

                
                frame.dispose();
                break;
            }
            JOptionPane.showMessageDialog(null, "Login unsuccessful.");
        }
    }

    /**
     * Prompts the instructor for his login information
     * and logs him into the system if he passes verification
     */
    public void instructorLogin()
    {
        Database db = new Database();
        while(true)
        {
            String ID = JOptionPane.showInputDialog(null, "Enter ID number:");
            try
            {
                if(ID.equals(null))
                    break;

            }catch(NullPointerException e){ break;}
            JPasswordField pass = new JPasswordField();
            JOptionPane.showMessageDialog ( null, pass, "Enter password", JOptionPane.OK_OPTION );
            char[] chars = pass.getPassword();
            String pw = new String(chars);
            if(db.verifyUser(ID, pw, 1))
            {
                //GUI gui = new GUI(ID, 1, db);
                System.out.println("Opened instructor");
                frame.dispose();
                break;
            }
            JOptionPane.showMessageDialog(null, "Login unsuccessful.");
        }        
    }

    /**
     * prompts the student for his ID number and logs him into the system
     * where he can view lectures.
     */
    public void studentLogin()
    {
        Database db = new Database();
        while(true)
        {
            String ID = JOptionPane.showInputDialog(null, "Enter ID number:");
            try
            {
                if(ID.equals(null))
                    break;

            }catch(NullPointerException e){ break;}
            if(db.verifyUser(ID, "", 0))
            {
                //GUI gui = new GUI(ID, 0, db);
                System.out.println("Opened student");
                frame.dispose();
                break;
            }
            JOptionPane.showMessageDialog(null, "Login unsuccessful.");
        }
    }

    //Used for mouse clicks.
    public void actionPerformed(ActionEvent e) {
        if ("administrator".equals(e.getActionCommand()))
        {
            adminLogin();
        }
        else if("instructor".equals(e.getActionCommand()))
        {
            instructorLogin();
        }
        else if("student".equals(e.getActionCommand()))
        {
            studentLogin();
        }

    }
}
